/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainGUI.java
 *
 * Created on 28/08/2009, 8:52:07 AM
 */

package jirc;

import java.util.ArrayList;
import javax.swing.JDesktopPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import jirc.dialog.AddServerDialog;

/**
 *
 * @author David
 */
public class MainGUI extends javax.swing.JFrame {
	private ArrayList<ServerWithWnd> serverList = new ArrayList<ServerWithWnd>();

    /** Creates new form MainGUI */
    public MainGUI() {
        initComponents();
		setLocationRelativeTo(null);
    }

	public ArrayList<ServerWithWnd> getServerList() {
		return serverList;
	}

	public JDesktopPane getContentPanel() {
		return contentPanel;
	}

	public void reloadServerList() {
		DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("Server List");
		for (ServerWithWnd server : serverList) {
			DefaultMutableTreeNode serverNode = new DefaultMutableTreeNode(server.server.getServer());
			final String[] channels = server.server.getChannels();
			for (String c : channels) {
				serverNode.add(new DefaultMutableTreeNode(c));
			}
			parentNode.add(serverNode);
		}
		serverTree.setModel(new DefaultTreeModel(parentNode));
	}

	public void processMessage(IRCServer sendServer, String channel, String sender, String login, String hostname, String message) {
		ServerWithWnd useServer = null;
		for (ServerWithWnd server : serverList) {
			if (server.server == sendServer) {
				useServer = server;
				break;
			}
		}
		if (useServer != null) {
			for (final ChannelWindow cWndo : useServer.wnd.getChannelWindows().values()) {
				if (cWndo.getChannel().equalsIgnoreCase(channel)) {
					cWndo.dropMessage(sender, message);
				}
			}
		}
	}

	public void processAction(IRCServer sendServer, String channel, String sender, String login, String hostname, String message) {
		ServerWithWnd useServer = null;
		for (ServerWithWnd server : serverList) {
			if (server.server == sendServer) {
				useServer = server;
				break;
			}
		}
		if (useServer != null) {
			for (final ChannelWindow cWndo : useServer.wnd.getChannelWindows().values()) {
				if (cWndo.getChannel().equals(channel)) {
					cWndo.dropAction(sender, message);
				}
			}
		}
	}

	public void processServerMsg(IRCServer sendServer, String line) {
		ServerWithWnd useServer = null;
		for (ServerWithWnd server : serverList) {
			if (server.server == sendServer) {
				useServer = server;
				break;
			}
		}
		if (useServer != null) {
			if (useServer.wnd != null) {
				useServer.wnd.dropMessage(line);
			}
		}
	}

	public void addServer(String hostname, int port, String password) {
		for (ServerWithWnd server : serverList) {
			if (server.server.getServer().equals(hostname)) {
				return; //Already added
			}
		}
		IRCServer addServer = new IRCServer();
		addServer.setAutoNickChange(true);
		try {
			addServer.connect(hostname, port, password);
		} catch (Exception err) {
			err.printStackTrace();
			return;
		}
		ServerWithWnd server = new ServerWithWnd();
		server.server = addServer;
		serverList.add(server);
		ServerWindow serverWin = new ServerWindow(addServer, addServer.getServer());
		serverWin.setVisible(true);
		server.wnd = serverWin;
		contentPanel.add(serverWin);
		reloadServerList();
	}

	public static final class ServerWithWnd {
		public IRCServer server;
		public ServerWindow wnd;
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolbar = new javax.swing.JToolBar();
        addServerBtn = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        serverTree = new javax.swing.JTree();
        contentPanel = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 400));

        toolbar.setFloatable(false);
        toolbar.setRollover(true);

        addServerBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jirc/res/book_add.png"))); // NOI18N
        addServerBtn.setToolTipText("Add a Server..");
        addServerBtn.setFocusable(false);
        addServerBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addServerBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addServerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addServer(evt);
            }
        });
        toolbar.add(addServerBtn);

        jSplitPane1.setDividerLocation(150);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Server List");
        serverTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(serverTree);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 149, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 578, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel2);

        contentPanel.setBackground(new java.awt.Color(225, 225, 225));
        jSplitPane1.setRightComponent(contentPanel);

        jMenu1.setText("File");
        menuBar.add(jMenu1);

        jMenu2.setText("Edit");
        menuBar.add(jMenu2);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolbar, javax.swing.GroupLayout.DEFAULT_SIZE, 907, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 907, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void addServer(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addServer
		AddServerDialog dialog = new AddServerDialog();
		dialog.setVisible(true);
	}//GEN-LAST:event_addServer


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addServerBtn;
    private javax.swing.JDesktopPane contentPanel;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTree serverTree;
    private javax.swing.JToolBar toolbar;
    // End of variables declaration//GEN-END:variables

}
